package de.ur.iw.wiki.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import de.ur.iw.wiki.weighters.WeightInfo;

public class WikiWeighterWizard extends JDialog implements ActionListener, FocusListener{
	
	private static final int COLUMNS = 3;
	private static final String NEXT = "Weiter";
	private static final String SAVE = "Speichern";
	private static final int DEFAULT_WINDOW_HEIGHT = 480;
	private static final int DEFAULT_WINDOW_WIDTH = 860;
	
	private WeightInfo info;
	private JPanel firstPart, secondPart;
	private JTextField overview,firstParagraphs, middleParagraphs, lastParagraphs, firstLinks, middleLinks, lastLinks;
	private JTextArea imgOverview,imgFirstPara, imgMidPara, imgLastPara, imgFirstLink, imgMidLink, imgLastLink;
	private JLayeredPane compPaneFirst, compPaneSecond;
	
	private Color curFocus = new Color(255,0,0,90);
	private Color noFocus = new Color(0,0,0,0);

	public WikiWeighterWizard(WikiGUI parent, WeightInfo info) {
		super(parent);
		this.setModalityType(ModalityType.APPLICATION_MODAL);
		this.info = info;
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		displayFirstPart();
		setSize(DEFAULT_WINDOW_WIDTH, DEFAULT_WINDOW_HEIGHT);
	    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
	    setLocationRelativeTo(null);

	}
	
	public void display(){
		setVisible(true);
	}
	
	private void displayFirstPart(){
		firstPart = new JPanel();
		firstPart.setLayout(new FlowLayout());
		setContentPane(firstPart);
		
		compPaneFirst = new JLayeredPane();
		JPanel identifierPanel = new JPanel();
		JPanel imagePanel = new JPanel();
		JPanel textPanel = new JPanel();
		JPanel navPanel = new JPanel();
		
		
		compPaneFirst.setPreferredSize(new Dimension(800,360));
		compPaneFirst.setVisible(true);
		
		identifierPanel.setOpaque(true);
		identifierPanel.setBackground(new Color(Color.TRANSLUCENT, true));
		
		compPaneFirst.setBounds(0, 0, 800, 360);
		imagePanel.setBounds(0,0,800,360);
		identifierPanel.setBounds(0, 0, 799, 359);
		
		compPaneFirst.add(imagePanel, 1,0);
		compPaneFirst.add(identifierPanel,2,0);
		
		firstPart.add(compPaneFirst);
		firstPart.add(textPanel);
		firstPart.add(navPanel);
		
		BufferedImage myPicture = null;
		try {
			myPicture = ImageIO.read(new File("./wiki_complete_use.jpg"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
		
		imagePanel.add(picLabel);
		
		imgOverview = new JTextArea();
		imgFirstPara = new JTextArea();
		imgMidPara = new JTextArea();
		imgLastPara = new JTextArea();
		
		imgOverview.setPreferredSize(new Dimension(800,110));
		imgFirstPara.setPreferredSize(new Dimension(800,62));
		imgMidPara.setPreferredSize(new Dimension(800,92));
		imgLastPara.setPreferredSize(new Dimension(800,55));
		
		imgOverview.setBackground(noFocus);
		imgFirstPara.setBackground(noFocus);
		imgMidPara.setBackground(noFocus);
		imgLastPara.setBackground(noFocus);
		
		imgOverview.setFocusable(false);
		imgFirstPara.setFocusable(false);
		imgMidPara.setFocusable(false);
		imgLastPara.setFocusable(false);
		
		identifierPanel.add(imgOverview);
		identifierPanel.add(imgFirstPara);
		identifierPanel.add(imgMidPara);
		identifierPanel.add(imgLastPara);
		
		JButton next = new JButton();
		next.setText(NEXT);
		next.addActionListener(this);
		next.setActionCommand(NEXT);
		navPanel.add(next);
		
		firstParagraphs = new JFormattedTextField(NumberFormat.getInstance());
		firstParagraphs.setText("" + (int) (info.getWeightOfFirstParagraphs()*100));
		firstParagraphs.setColumns(COLUMNS);
		middleParagraphs = new JFormattedTextField(NumberFormat.getInstance());
		middleParagraphs.setText("" + (int) (info.getWeightOfMiddleParagraphs()*100));
		middleParagraphs.setColumns(COLUMNS);
		lastParagraphs = new JFormattedTextField(NumberFormat.getInstance());
		lastParagraphs.setText("" + (int) (info.getWeightOfLastParagraphs()*100));
		lastParagraphs.setColumns(COLUMNS);
		
		firstParagraphs.addFocusListener(this);
		middleParagraphs.addFocusListener(this);
		lastParagraphs.addFocusListener(this);
		
		textPanel.add(firstParagraphs);
		textPanel.add(middleParagraphs);
		textPanel.add(lastParagraphs);
		
		firstParagraphs.requestFocus();
	}
	
	private void displaySecondPart(){
		secondPart = new JPanel();
		secondPart.setLayout(new FlowLayout());
		setContentPane(secondPart);
		
		compPaneSecond = new JLayeredPane();
		JPanel identifierPanel = new JPanel();
		JPanel imagePanel = new JPanel();
		JPanel textPanel = new JPanel();
		JPanel navPanel = new JPanel();
		
		compPaneSecond.setPreferredSize(new Dimension(800,400));
		compPaneSecond.setVisible(true);
		
		identifierPanel.setOpaque(true);
		identifierPanel.setBackground(new Color(Color.TRANSLUCENT, true));
		
		compPaneSecond.setBounds(0, 0, 800, 400);
		imagePanel.setBounds(0, 0, 800,400);
		identifierPanel.setBounds(0, 0, 799, 399);
		
		compPaneSecond.add(imagePanel, 1,0);
		compPaneSecond.add(identifierPanel,2,0);
		
		secondPart.add(compPaneSecond);
		secondPart.add(textPanel);
		secondPart.add(navPanel);
			
		BufferedImage myPicture = null;
		try {
			myPicture = ImageIO.read(new File("./wiki_part_use.jpg"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JLabel picLabel = new JLabel(new ImageIcon( myPicture ));
		picLabel.setVisible(true);
		imagePanel.add(picLabel);
		
		imgFirstLink = new JTextArea();
		imgMidLink = new JTextArea();
		imgLastLink = new JTextArea();
		
		imgFirstLink.setFocusable(false);
		imgMidLink.setFocusable(false);
		imgLastLink.setFocusable(false);
		
		
		imgFirstLink.setPreferredSize(new Dimension(800,80));
		imgMidLink.setPreferredSize(new Dimension(800,220));
		imgLastLink.setPreferredSize(new Dimension(800,80));
		
		imgFirstLink.setBackground(noFocus);
		imgMidLink.setBackground(noFocus);
		imgLastLink.setBackground(noFocus);
		
		JTextArea spaceArea = new JTextArea();
		spaceArea.setFocusable(false);
		spaceArea.setBackground(noFocus);
		spaceArea.setPreferredSize(new Dimension(800,20));
		
		identifierPanel.add(spaceArea);
		identifierPanel.add(imgFirstLink);
		identifierPanel.add(imgMidLink);
		identifierPanel.add(imgLastLink);
		
		
		JButton save = new JButton();
		save.setText(SAVE);
		save.addActionListener(this);
		save.setActionCommand(SAVE);
		navPanel.add(save);
		
		firstLinks = new JFormattedTextField(NumberFormat.getInstance());
		firstLinks.setText("" + (int) (info.getWeightOfFirstLinks()*100));
		firstLinks.setColumns(COLUMNS);
		middleLinks = new JFormattedTextField(NumberFormat.getInstance());
		middleLinks.setText("" + (int) (info.getWeightOfMiddleLinks()*100));
		middleLinks.setColumns(COLUMNS);
		lastLinks = new JFormattedTextField(NumberFormat.getInstance());
		lastLinks.setText("" + (int) (info.getWeightOfLastLinks()*100));
		lastLinks.setColumns(COLUMNS);
		
		firstLinks.addFocusListener(this);
		middleLinks.addFocusListener(this);
		lastLinks.addFocusListener(this);
		
		textPanel.add(firstLinks);
		textPanel.add(middleLinks);
		textPanel.add(lastLinks);
		
		firstLinks.requestFocusInWindow();
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals(NEXT)){
			// get Data from TextFields and save in WeightInfo for First Part
			info.setWeightOfFirstParagraphs(Double.parseDouble(firstParagraphs.getText()) / 100);
			info.setWeightOfMiddleParagraphs(Double.parseDouble(middleParagraphs.getText()) / 100);
			info.setWeightOfLastParagraphs(Double.parseDouble(lastParagraphs.getText()) / 100);
			
			firstPart.setVisible(false);
			displaySecondPart();
		}
		
		if(e.getActionCommand().equals(SAVE)){
			// get Data from TextFields and save in WeightInfo for Second Part
			info.setWeightOfFirstLinks(Double.parseDouble(firstLinks.getText()) / 100);
			info.setWeightOfMiddleLinks(Double.parseDouble(middleLinks.getText()) / 100);
			info.setWeightOfLastLinks(Double.parseDouble(lastLinks.getText()) / 100);
			
			
			secondPart.setVisible(false);
			dispose();
		}
		
	}

	@Override
	public void focusGained(FocusEvent f) {
		// TODO Auto-generated method stub
		JTextField field = (JTextField) f.getComponent();
		
		if(field == firstParagraphs){
			imgFirstPara.setBackground(curFocus);
			compPaneFirst.repaint();
		}else if(field == middleParagraphs){
			imgMidPara.setBackground(curFocus);
			compPaneFirst.repaint();
		}else if(field == lastParagraphs){
			imgLastPara.setBackground(curFocus);
			compPaneFirst.repaint();
		}else if(field == firstLinks){
			imgFirstLink.setBackground(curFocus);
			compPaneSecond.repaint();
		}else if(field == middleLinks){
			imgMidLink.setBackground(curFocus);
			compPaneSecond.repaint();
		}else if(field == lastLinks){
			imgLastLink.setBackground(curFocus);
			compPaneSecond.repaint();
		}
		
		
		
	}

	@Override
	public void focusLost(FocusEvent f) {
		// TODO Auto-generated method stub
		JTextField field = (JTextField) f.getComponent();
		
		if(field == firstParagraphs){
			imgFirstPara.setBackground(noFocus);
			compPaneFirst.repaint();
		}else if(field == middleParagraphs){
			imgMidPara.setBackground(noFocus);
			compPaneFirst.repaint();
		}else if(field == lastParagraphs){
			imgLastPara.setBackground(noFocus);
			compPaneFirst.repaint();
		}else if(field == firstLinks){
			imgFirstLink.setBackground(noFocus);
			compPaneSecond.repaint();
		}else if(field == middleLinks){
			imgMidLink.setBackground(noFocus);
			compPaneSecond.repaint();
		}else if(field == lastLinks){
			imgLastLink.setBackground(noFocus);
			compPaneSecond.repaint();
		}
	}

}
